<?php

include_once APPLICATION_DIR . "/application/model/VIDEO.php";

class Video extends DataObjects_VIDEO
{
	function getList($filter = array(), &$pager=false)
	{
		$video = new Video();
		if (isset($filter['name']) && $filter['name']) {
			$video->whereAdd("VIDEO.TITLE LIKE '%".mysql_escape_string($filter['name'])."%' OR VIDEO.CONTENT LIKE '%".mysql_escape_string($filter['name'])."%'");
		}
        if (isset($filter['id_category']) && $filter['id_category']) {
            if ($filter['id_category']==-1){
                $video->whereAdd("( CATEGORIES IS NULL OR CATEGORIES = '' )");
            }else{
                $video->whereAdd("FIND_IN_SET('{$filter['id_category']}', CATEGORIES)");
            }    
		}
		if (isset($filter['id_provider']) && $filter['id_provider']) {
			$video->ID_PROVIDER =  $filter['id_provider'];
		}
        if (isset($filter['published_date']) && $filter['published_date']) {
            $video->whereAdd(" DATE(PUBLISHED_DATE) >= '".Utils::convertDateToMySQLStyle($filter['published_date'])."'");
        }
        
        if (isset($filter['featured']) && $filter['featured']) {
            $video->FEATURED = $filter['featured'];
        }
        if (isset($filter['disabled'])) {
            $video->DISABLED = $filter['disabled'];
        } else {
            $video->DISABLED = 0;
        }
		if (isset($filter['id_video']) && $filter['id_video']) {
			$video->ID = $filter['id_video'];
		}
        $video->joinAdd(new Video_Provider());
        $video->joinAdd(new Login());
		$video->selectAdd();
        $video->selectAdd("VIDEO.*, VIDEO_PROVIDER.NAME PROVIDER, LOGIN.FIRST_NAME, LOGIN.LAST_NAME");
        $video->orderBy("VIDEO.CREATED_DATE DESC, TITLE");
		if ($pager == true) {
			$pager = new DBPager($video, $filter, true);
		}
		$video->find();

		return $video;
	}

	public function isExistsByName($id_provider = 0, $id= 0, $title = '')
	{
		if ($title == '')
		{
			return false;
		}	
		$video = new Video();
		if ($id) {
			$video->whereAdd("ID <> {$id}");
		}
        $video->whereAdd("TITLE LIKE '%".trim($title)."%'");
        $video->find();
		return $video->N > 0;
	}
    
    function getDetail($id)
	{
		$video = new Video();
		$video->joinAdd(new Video_Provider());
        $video->joinAdd(new Login());
		$video->ID = $id;
		$video->selectAdd();
		$video->selectAdd("VIDEO.*, VIDEO_PROVIDER.NAME AS PROVIDER_NAME, LOGIN.FIRST_NAME, LOGIN.LAST_NAME");
		$video->find(1);
		return $video;
	}
    
     public function markFeatured($id = 0, $featured = 0) {
		$video = new Video();
		if (!$id || !$video->get($id)) {
			return '0';
		}
		$video->FEATURED = $featured;
		$video->update();
		
		return '1';
	}
    
    public function disabledVideo($id = 0, $value = 0) {
		$video = new Video();
		if (!$id || !$video->get($id)) {
			return '0';
		}
		$video->DISABLED = $value;
		$video->update();
		
		return '1';
	}
    
    public function validateInput(){
		global $error_msg;

        if (!textvalid($this->TITLE)) {
			$error_msg->add(_MSG_TITLE_NOT_EMPTY);
		}
		if(!Utils::isValidLength($this->TITLE, MAX_LENGTH_NAME))
		{
			$error_msg->add(_MSG_TITLE_IS_TOO_LONG);
		}       
        if (self::isExistsByName($this->ID_PROVIDER, $this->ID, $this->TITLE))
		{
			$error_msg->add(_MSG_VIDEO_EXISTS);
		}
        
        if (!textvalid($this->ID_PROVIDER)) {
            $error_msg->add(_MSG_PROVIDER_NOT_EMPTY);
        } else {
            $video_provider = new Video_Provider();
            if (!$menu->get($this->ID_PROVIDER)) {
                $error_msg->add(_MSG_PROVIDER_NOT_EXISTS);
            }
        }
        
        if (is_array($this->CATEGORIES) && count($this->CATEGORIES) <= 0)
		{
            $error_msg->add(_MSG_NO_CATEGORY_SELECTED);
        }
        else
        {
            if (!Category::isExistsByIds($this->CATEGORIES)) {
                $error_msg->add(_MSG_CATEGORY_INVALID);
            }
		}
        $this->CATEGORIES = implode(',', $this->CATEGORIES);
        
        if (!textvalid($this->AUTHOR)) {
            $error_msg->add(_MSG_AUTHOR_NOT_EMPTY);
        } else {
            $author = new Login();
            if (!$author->get($this->AUTHOR)) {
                $error_msg->add(_MSG_AUTHOR_NOT_EXISTS);
            }
        }
        
        if (!textvalid($this->IMAGE)) {
            $error_msg->add(_MSG_IMAGE_NOT_EMPTY);
        }
        
        if (textvalid($this->IMAGE) && !Tags::isExists($this->TAGS)) {
            $error_msg->add(_MSG_TAGS_INVALID);
        }
        
        if (!textvalid($this->TYPE)) {
            $error_msg->add(_MSG_TYPE_NOT_EMPTY);
        }
        
		return $error_msg->isNoError();
	}

    function deleteLogic()
	{
		if (!$this->DISABLED) {
			$this->DISABLED = IS_DISABLED;
			$this->STATUS = STATUS_TO_SYNCHRONIZE;
			$this->update();
		}
	}
}
